﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ui="clr-namespace:Bonobo.Irc.Client.UI"
    xmlns:data="clr-namespace:Bonobo.Irc.Client.UI.Data"              
    x:Class="Bonobo.Irc.Client.UI.Data.IrcServerConversationTemplate">

    <DataTemplate x:Key="IrcServerConversationContentTemplate" >
        <Grid GotFocus="Test">
            <Grid.Resources>
                <data:IPAddressConverter x:Key="IPAddressConverter" />
                <data:FormattingConverter x:Key="Formatter" />
                <data:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
                <data:ServerResponseConverter x:Key="ServerResponseConverter" />
            </Grid.Resources>
            <Grid.CommandBindings>
                <CommandBinding Command="{x:Static ui:IrcCommands.Connect}" 
                                CanExecute="ConnectCanExecute"
                                Executed="ConnectExecuted" />
            </Grid.CommandBindings>
            <Grid>
                <Grid.Style>
                    <Style TargetType="{x:Type Grid}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Session.State}" Value="Closed">
                                <Setter Property="Visibility" Value="Collapsed" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Session.State}" Value="ServerError">
                                <Setter Property="Visibility" Value="Collapsed" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Grid.Style>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition Width="200px" />
                </Grid.ColumnDefinitions>
                <ListBox Name="lvItems"
                         BorderThickness="0"
                         ItemsSource="{Binding Messages}"
                         SelectionMode="Single"
                         ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                         Grid.Column="0"
                         Padding="5"
                         >
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid Margin="0 2px 0 2px">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="100px" />
                                    <ColumnDefinition />
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="{Binding TimeStamp, 
                                                Converter={StaticResource Formatter}, 
                                                ConverterParameter=' \{0:dd.MM.yyyy hh:mm\}'}"
                                           Grid.Column="0"
                                           TextWrapping="Wrap"
                                           Foreground="Gray"
                                           Margin="0 0 5px 0"
                                           Width="150px" />
                                <TextBlock TextWrapping="Wrap"
                                           Grid.Column="1"
                                           Text="{Binding Converter={StaticResource ServerResponseConverter}}">                                    
                                    <TextBlock Margin="5px 0 0 0" Visibility="{Binding IsLoginError, Converter={StaticResource BoolToVisibilityConverter}}">
                                        <Hyperlink Command="{x:Static ui:IrcCommands.Disconnect}">  
                                            Relog                                  
                                        </Hyperlink>
                                    </TextBlock>                                    
                                </TextBlock>
                            </Grid>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
                <ListBox Grid.Column="1">
                    Ahoj, Johny
                </ListBox>
            </Grid>


            <Grid>
                <Grid.Style>
                    <Style TargetType="{x:Type Grid}">
                        <Setter Property="Visibility" Value="Collapsed" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Session.State}" Value="Closed">
                                <Setter Property="Visibility" Value="Visible" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Session.State}" Value="ServerError">
                                <Setter Property="Visibility" Value="Visible" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Grid.Style>
                <Grid.Resources>
                    <DataTemplate DataType="{x:Type ValidationError}">
                        <TextBlock FontStyle="Italic" 
                                           Foreground="Red"          
                                           HorizontalAlignment="Left"
                                           Margin="4"
                                           Text="{Binding Path=ErrorContent}" />
                    </DataTemplate>
                </Grid.Resources>
                <Grid.RowDefinitions>
                    <RowDefinition Height="27" />
                    <RowDefinition Height="27" />
                    <RowDefinition Height="27" />
                    <RowDefinition Height="27" />
                    <RowDefinition Height="27" />
                    <RowDefinition Height="27" />
                    <RowDefinition Height="27" />
                    <RowDefinition Height="150" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="15" />
                    <ColumnDefinition Width="150" />
                    <ColumnDefinition Width="200" />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Label Grid.Column="1" 
                               Grid.Row="1">Server Address</Label>
                <TextBox Grid.Column="2" 
                                 Grid.Row="1" 
                                 x:Name="txtServerAddress"
                                 Margin="2">
                    <Binding Path="Session.ConnectionInfo.Address" Converter="{StaticResource IPAddressConverter}" UpdateSourceTrigger="Explicit">
                        <Binding.ValidationRules>
                            <data:RegexValidationRule Pattern="^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$" 
                                                            ErrorMessage="Address is required and should look like xxx.xxx.xxx.xxx" />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox>
                <ContentPresenter Content="{Binding ElementName=txtServerAddress, Path=(Validation.Errors).CurrentItem}"
                                          Grid.Column="3"
                                          Grid.Row="1"/>

                <Label Grid.Column="1"
                               Grid.Row="2">Port</Label>
                <TextBox Grid.Column="2" 
                                 Grid.Row="2" 
                                 Margin="2" 
                                 x:Name="txtPort">
                    <Binding Path="Session.ConnectionInfo.Port" UpdateSourceTrigger="Explicit">
                        <Binding.ValidationRules>
                            <data:IntegerRangeValidationRule MinValue="1" MaxValue="65535" ErrorMessage="Port must be between 1-65535" />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox>
                <ContentPresenter Content="{Binding ElementName=txtPort, Path=(Validation.Errors).CurrentItem}"
                                          Grid.Column="3"
                                          Grid.Row="2"/>

                <Label Grid.Column="1" 
                               Grid.Row="3">Username</Label>
                <TextBox x:Name="txtUsername"
                                 Margin="2" 
                                 Grid.Column="2" 
                                 Grid.Row="3">
                    <Binding Path="Session.ConnectionInfo.Username" UpdateSourceTrigger="Explicit">
                        <Binding.ValidationRules>
                            <data:StringRequiredValidationRule ErrorMessage="Username is required." />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox>
                <ContentPresenter Content="{Binding ElementName=txtUsername, Path=(Validation.Errors).CurrentItem}"
                                          Grid.Column="3"
                                          Grid.Row="3"/>

                <Label Grid.Column="1" 
                               Grid.Row="4">Password</Label>
                <TextBox Margin="2" 
                                 x:Name="txtPassword" 
                                 Grid.Column="2" 
                                 Grid.Row="4">
                    <Binding Path="Session.ConnectionInfo.ConnectionPassword" UpdateSourceTrigger="Explicit">
                    </Binding>
                </TextBox>


                <Label Grid.Column="1"
                               Grid.Row="5">Full Name</Label>
                <TextBox Grid.Column="2" 
                                 Grid.Row="5" 
                                 Margin="2" 
                                 x:Name="txtFullName">
                    <Binding Path="Session.ConnectionInfo.Realname" UpdateSourceTrigger="Explicit">
                        <Binding.ValidationRules>
                            <data:StringRequiredValidationRule ErrorMessage="Full Name is required." />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox>
                <ContentPresenter Content="{Binding ElementName=txtFullName, Path=(Validation.Errors).CurrentItem}"
                                          Grid.Column="3"
                                          Grid.Row="5"/>

                <Label Grid.Column="2" 
                               Grid.Row="7" 
                               Margin="2" 
                               Foreground="Red">
                    <Label.Style>
                        <Style TargetType="{x:Type Label}">
                            <Setter Property="Visibility" Value="Collapsed" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Session.State}" Value="ServerError">
                                    <Setter Property="Visibility" Value="Visible" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Label.Style>
                    Server connection error.
                </Label>
                <Button Grid.Column="2" 
                        Grid.Row="6" 
                        HorizontalAlignment="Right" 
                        Margin="2" 
                        Name="btnConnect" 
                        Width="75" 
                        ToolTip="Connect to server"
                        Command="{x:Static ui:IrcCommands.Connect}"
                        CommandParameter="{Binding}">
                    _Connect
                </Button>

            </Grid>
        </Grid>
    </DataTemplate>

</ResourceDictionary>